package com.prd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.CheckStatus;
import com.prd.mapper.EntNarrationSummaryMapper;
import com.prd.model.EntNarrationRecord;
import com.prd.model.EntNarrationSummary;
import com.prd.model.LoginUser;
import com.prd.service.EntNarrationSummaryService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntNarrationSummaryServiceImpl extends ServiceImpl<EntNarrationSummaryMapper, EntNarrationSummary> implements EntNarrationSummaryService {

    @Autowired
    private EntNarrationSummaryMapper entNarrationSummaryMapper;

    @Override
    public int insertInfo(EntNarrationSummary entNarrationSummary) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entNarrationSummary);
        entNarrationSummary.setStatus(CheckStatus.ONE.getCode());
        entNarrationSummary.setPerform(1);
        entNarrationSummary.setNumber(String.valueOf(System.nanoTime()));
        return entNarrationSummaryMapper.insert(entNarrationSummary);
    }

    @Override
    public int updateInfo(EntNarrationSummary entNarrationSummary) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entNarrationSummary);
        return entNarrationSummaryMapper.updateById(entNarrationSummary);
    }

    @Override
    public int checkRecord(Long summaryId) {
        EntNarrationSummary entNarrationSummary = entNarrationSummaryMapper.selectById(summaryId);
        if (entNarrationSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entNarrationSummary.setStatus(CheckStatus.TWO.getCode());
        return updateInfo(entNarrationSummary);
    }
}
